package com.lsmo.android.carfans.view.car;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.activity.car.CarPriceRangeListActivity;
import com.lsmo.android.carfans.process.car.CarProcess;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

/**
 * @author SYX
 * @类描述：  车型中价格Tab                  
 * @项目名称：carfans     
 * @类名称：SelectPriceView                    
 * @修改时间：  
 * @修改备注： 
 */
public class SelectPriceView extends LinearLayout implements ViewProcessCallBack  {
	public static final String TAG = SelectPriceView.class.getSimpleName();
	
	private CarProcess carProcess;
	private String[] strs;
	private ListView listView;
	private LinearLayout linearLayout1,linearLayout2;
	private Context mContext ;
	private ViewGroup mProgressBarLayout ;

	public SelectPriceView(Context context) {
		super(context);
		this.carProcess = new CarProcess();
		this.mContext = context;
		
		((Activity)context).getLayoutInflater().inflate(R.layout.car_main_selectcar_layout, this);
		this.postInvalidate();
		listView = (ListView)this.findViewById(R.id.listView);//listview
		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
		//暂时隐藏不用的布局
		linearLayout1 = (LinearLayout)this.findViewById(R.id.button_relative);
		linearLayout2  = (LinearLayout)this.findViewById(R.id.navigation_line_id);
		linearLayout1.setVisibility(View.GONE);
		linearLayout2.setVisibility(View.GONE);
	}
	
	@Override
	public void processAfterTaskFinish(int code, Object result ) {
		//选择品牌
		if(code == SelectPriceTask.GET_PRICE_LIST){
			if(result!=null){
				setPrice();//价格
			}
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.listView.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void updateView() {
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		// TODO Auto-generated method stub
		
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.listView.setVisibility(View.GONE);
		
			new SelectPriceTask(SelectPriceTask.GET_PRICE_LIST,
					this.mContext,
					this,
					this.carProcess 
		 ).execute(new String[]{});
	}
	
	private class SelectPriceTask extends BaseTask{
		public static final int GET_PRICE_LIST = 3;//价格
		private CarProcess carProcess;
		
		public SelectPriceTask(int code , Context context,ViewProcessCallBack view,BaseProcess process ) {
			super(code,context,view );
			this.carProcess = (CarProcess) process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    //选择价格
				case GET_PRICE_LIST:
					result = "";
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				//选择价格
				case GET_PRICE_LIST:
				 	mView.processAfterTaskFinish(this.mCode ,result );
				 	break;
		   }
			 
		}
	}
	
	/**
	 * listview 监听事件
	 * @param method
	 * @param param
	 */
	private OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int position,
				long time) {
			// TODO Auto-generated method stub
			String name = strs[position];
			CarPriceRangeListActivity.launch(mContext, position, position, name);
		}
	};
	
	//价格
	public void setPrice(){
		strs = getResources().getStringArray(R.array.select_by_price_array);
	    ArrayAdapter localArrayAdapter = new ArrayAdapter(mContext, 
	    		R.layout.car_adapter_price, R.id.price_text, strs);
	    listView.setAdapter(localArrayAdapter);
	    listView.setOnItemClickListener(itemClickListener);//监听事件
	}	

}
